package com.xo.xodemo.util;

import com.github.pagehelper.util.StringUtil;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
public class FileUtil {
//     负责把提交上来的文件生成一个不重复的名字存起来
    public  static void  transfer   (MultipartFile file,String rootpath) throws IllegalStateException, IOException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM");
        String tmp=simpleDateFormat.format(new Date())+"/";
        File savepath = new File(rootpath,tmp);
        if (!savepath.exists()) {
            savepath.mkdirs();
        }
        String fileName=file.getOriginalFilename();
        if(!StringUtil.isEmpty(fileName)){
            String suffix= FilenameUtils.getExtension(fileName);
            String newFileName = UUID.randomUUID().toString().toLowerCase()+"."+suffix;
            tmp=tmp+"/"+newFileName;
            File targetFile = new File(savepath,newFileName);
            file.transferTo(targetFile);

        }
    }
}
